
#import "SettingsTableViewController.h"
#import "Reactor.h"
#import "SettingsTableViewCell.h"
#import "Settings.h"
#import "YStepperView.h"
#import "NKColorSwitch.h"
#import "GamesToPlayTableViewController.h"

@interface SettingsTableViewController ()

@property Settings *settings;
@end

@implementation SettingsTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.settings = [Settings sharedSettings];
    
    [self.navigationController setNavigationBarHidden:NO];
    
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    
    //set nav button
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 40, 40)];
    [backButton setContentMode:UIViewContentModeScaleAspectFit];
    [backButton setBackgroundImage:[UIImage imageNamed:@"backArrow.png"] forState:UIControlStateNormal];
    UIBarButtonItem *navBackButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = navBackButton;
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SettingsTableViewCell" bundle:nil] forCellReuseIdentifier:@"settingsCell"];
    
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.navigationController.navigationBar.bounds = CGRectMake(0,0,768,100);
}
-(void)backButtonPressed:(BOOL)pressed {
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL)prefersStatusBarHidden
{ return YES;
}

- (void)showSelectGames:(NSNotification *) notification
{
    GamesToPlayTableViewController *gtpVC = [[GamesToPlayTableViewController alloc] initWithNibName:@"GamesToPlayTableViewController" bundle:nil];
    
    [self.navigationController pushViewController:gtpVC animated:YES];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *settingsIdentifier = @"settingsCell";
    
    SettingsTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:settingsIdentifier];
    if (cell == nil) {
        cell =  [[SettingsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:settingsIdentifier];
        
    }
    //games cell
    if (indexPath.row == 0) {
        
        cell.label.text = @"Games";
        cell.subLabel.text = @"Choose which games to be included";
        
        UIButton *gamesButton = [UIButton buttonWithType:UIButtonTypeCustom];
        gamesButton.frame = CGRectMake(70, 20, 70, 70);
        
        [gamesButton setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];
        
        [gamesButton addTarget:self action:@selector(showSelectGames:) forControlEvents:UIControlEventTouchUpInside];
        [cell.control addSubview:gamesButton];
        
        
    }
    //rounds
    else if (indexPath.row == 1)
    {
        cell.label.text = @"Rounds";
        cell.subLabel.text = @"How many times to repeat each game";
        
        YStepperView *yStepper = [[YStepperView alloc] initWithFrame:CGRectMake(20, 20, 180.0, 70.0)];
        [yStepper setStepperColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.75] withDisableColor:nil];
        [yStepper setTextColor:[UIColor whiteColor]];
        [yStepper setTextLabelFont:[UIFont systemFontOfSize:25.0f]];
        [yStepper setStepperRange:1 andMaxValue:7];
        [yStepper setValue:self.settings.rounds];
        
        [cell.control addSubview:yStepper];
        
    }
    //score
    else if (indexPath.row == 2) {
        
        cell.label.text = @"Score";
        cell.subLabel.text = @"Show score during game";
        
        NKColorSwitch *score = [[NKColorSwitch alloc] initWithFrame:CGRectMake(40, 20, 120, 60)];
        [score addTarget:self action:@selector(switchPressed:) forControlEvents:UIControlEventValueChanged];
        [score setTintColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.2]];
        [score setOnTintColor:[UIColor redColor]];
        [score setThumbTintColor:[UIColor whiteColor]];
        [score setShape:kNKColorSwitchShapeRectangle];
        
        if (self.settings.showScore)
            score.on = YES;
        else
            score.on = NO;
        
        [cell.control addSubview:score];
        
        
    }
    //sounds
    else {
        
        cell.label.text = @"Sounds";
        cell.subLabel.text = @"Play sounds during game";
        
        NKColorSwitch *sounds = [[NKColorSwitch alloc] initWithFrame:CGRectMake(40, 20, 120, 60)];
        [sounds addTarget:self action:@selector(switchPressed:) forControlEvents:UIControlEventValueChanged];
        [sounds setTintColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.2]];
        [sounds setOnTintColor:[UIColor redColor]];
        [sounds setThumbTintColor:[UIColor whiteColor]];
        [sounds setShape:kNKColorSwitchShapeRectangle];
        
        if (self.settings.sounds)
            sounds.on = YES;
        else
            sounds.on = NO;
        
        [cell.control addSubview:sounds];
        
        
    }
    return cell;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    //Rounds cell
    SettingsTableViewCell* getRoundsCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    YStepperView *stepper = getRoundsCell.control.subviews[0];
    [self.settings changeNubmerOfRounds:[stepper getValue]];
    
    //Score cell
    SettingsTableViewCell* getScoreCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    NKColorSwitch *scoreSwitch = getScoreCell.control.subviews[0];
    self.settings.showScore = scoreSwitch.isOn;
    
    //Sounds cell
    SettingsTableViewCell* getSoundCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    NKColorSwitch *soundSwitch = getSoundCell.control.subviews[0];
    self.settings.sounds = soundSwitch.isOn;
    
    //SAVE TO USER DEFAULTS
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:[NSNumber numberWithBool:self.settings.showScore]
                 forKey:@"showScore"];
    [defaults setObject:[NSNumber numberWithBool:self.settings.sounds] forKey:@"sounds"];
    [defaults setInteger:self.settings.rounds forKey:@"rounds"];
    NSLog(@"Score: %d Rounds: %d Sounds: %d", self.settings.showScore, self.settings.rounds, self.settings.sounds);
    
}

- (void)switchPressed:(id)sender
{
    NKColorSwitch *nkswitch = (NKColorSwitch *)sender;
    if (nkswitch.isOn)
        self.settings.sounds = YES;
    else
        NSLog(@"switchPressed OFF");
}





@end
